package com.jrx.anytxn.transaction.job.paph.step;

import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.service.product.IProductTableService;
import com.jrx.anytxn.transaction.bean.PaphFileBean;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlLoanFailLog;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class GenerateLoanFailProcesser implements ItemProcessor<TlLoanFailLog, PaphFileBean> {

    @Autowired
    private IProductTableService productTableService;

    @Override
    public PaphFileBean process(TlLoanFailLog log) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append(TransBizConstant.FIRST_WORD);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //申请号
        sb.append(log.getContractNumber());
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //合作渠道
        sb.append(TransBizConstant.INSURE_COMPANY);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //借据号
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //客户姓名
        sb.append(log.getCustomerName());
        sb.append(TransBizConstant.FILE_SEPARATOR);
        //放款时间
        sb.append(log.getLoanDate());
        sb.append(TransBizConstant.FILE_SEPARATOR);
        sb.append(TransBizConstant.FAIL_LOANAMT);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        sb.append(TransBizConstant.FAIL_BUSINESS_RATE);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        sb.append(TransBizConstant.FAIL_LOANAMT);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        sb.append(TransBizConstant.LOAN_FAILED);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        sb.append(TransBizConstant.TRANSFER_FAILED);
        sb.append(TransBizConstant.FILE_SEPARATOR);
        sb.append(log.getErrorCode());
        sb.append(TransBizConstant.FILE_SEPARATOR);
        sb.append(log.getErrorMsg());
        sb.append(TransBizConstant.FILE_SEPARATOR);
        sb.append(log.getSeqNo());
        sb.append(TransBizConstant.LINE_END);

        PaphFileBean paphFileBean = new PaphFileBean();
        paphFileBean.setFileString(sb.toString());
        String productId = log.getProductId();
        PrProductTable product = productTableService.findProByProIdAndOrgId
                (productId,TransBizConstant.DEFAULT_ORG_ID,log.getTenantId());
        paphFileBean.setProductType(product.getTableType());
        return paphFileBean;

    }
}